Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller.
The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them.
My intention is to mark these examples with [DSF].
You should skim through Chapter 10 to see what kinds of things are available, so that you'll know they are there if you need them.
You can read the details later.
innerHTML
hackingWhy not just code the previous example this way?
function slideClick() { $("thisslide").innerHTML += "<p>A paragraph!</p>"; }
"
and '
function slideClick() { $("main").innerHTML += "<p style='color: red; " + "margin-left: 50px;' " + "onclick='myOnClick();'>" + "A paragraph!</p>"; }
innerHTML
function slideClick() { var p = document.createElement("p"); p.className = "special"; p.onclick = myOnClick; p.innerHTML = "A paragraph!"; $("thisslide").appendChild(p); }
.special { color: red; margin-left: 50px; }
<button id="clickme">Click Me</button>
window.onload = function() { $("clickme").onclick = biggerFont; }; function biggerFont() { var size = parseInt($("clickme").style.fontSize); size += 4; $("clickMe").style.fontSize = size + "pt"; }
style
property lets you set any CSS style for an element
function biggerFont() {
// make the text bigger
var size = parseInt($("clickme").getStyle("font-size"));
$("clickme").style.fontSize = (size + 4) + "pt";
}
getStyle
method (added to each DOM object by Prototype) allows accessing existing styles$("main").style.top = $("main").getStyle("top") + 100 + "px";// bad!
"200px" + 100 + "px"
, "200px100px"
$("main").style.top = parseInt($("main").getStyle("top")) + 100 + "px"; // correct
function highlightField() {
// turn text yellow and make it bigger
if (!$("text").className) {
$("text").className = "highlight";
} else if ($("text").className.indexOf("invalid") < 0) {
$("text").className += " highlight";
}
}
className
property corresponds to HTML class
attribute
function highlightField() {
// turn text yellow and make it bigger
if (!$("text").hasClassName("invalid")) {
$("text").addClassName("highlight");
}
}
addClassName
,
removeClassName
,
toggleClassName
,
hasClassName
manipulate CSS classesclassName
DOM property, but don't have to manually split by spaces
classList
property, allows similar manipulations, but is clunkier.
Scriptaculous : a JavaScript library, built on top of Prototype, that adds:
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js" type="text/javascript"></script> <script src="http://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js" type="text/javascript"></script>
.js
files to a folder
documentation available on the Scriptaculous wiki
Online API documentation is incomplete (argh), but the following are useful resources:
(appearing)
(disappearing)
(Getting attention)
element.effectName(); // for most effects // some effects must be run the following way: new Effect.name(element or id);
$("sidebar").shake(); var buttons = $$("results > button"); for (var i = 0; i < buttons.length; i++) { buttons[i].fade(); }
element.effectName( { option: value, option: value, ... } );
$("my_element").pulsate({ duration: 2.0, pulses: 2 });
{}
)delay
,
direction
,
duration
,
fps
,
from
,
queue
,
sync
,
to
,
transition
$("my_element").fade({ duration: 3.0, afterFinish: displayMessage }); function displayMessage(effect) { alert(effect.element + " is done fading now!"); }
beforeStart
,
beforeUpdate
,
afterUpdate
,
afterFinish
afterFinish
event fires once the effect is done animating
Effect
object as its parameter
element
, options
, currentFrame
, startOn
, finishOn
Shrink
) are technically "parallel effects", so to access the modified element, you write effect.effects[0].element
rather than just effect.element
Scriptaculous provides several objects for supporting drag-and-drop functionality:
Draggable
: an element that can be draggedDraggables
: manages all Draggable
objects on the pageDroppables
: elements on which a Draggable
can be droppedSortable
: a list of items that can be reorderedDraggable
new Draggable(element or id, { options } );
handle
,
revert
,
snap
,
zindex
,
constraint
,
ghosting
,
starteffect
,
reverteffect
,
endeffect
onStart
,
onDrag
,
onEnd
Draggable
object, and the mouse eventDraggable
example<div id="draggabledemo1">Draggable demo. Default options.</div> <div id="draggabledemo2">Draggable demo. {snap: [40,40], revert: true}</div>
document.observe("dom:loaded", function() { new Draggable("draggabledemo1"); new Draggable("draggabledemo2", {revert: true, snap: [40, 40]}); });
Draggables
drags
,
observers
register
,
unregister
,
activate
,
deactivate
,
updateDrag
,
endDrag
,
keyPress
,
addObserver
,
removeObserver
,
notify
Droppables
Droppables.add(element or id, { options } );
accept
,
containment
,
hoverclass
,
overlap
,
greedy
onHover
,
onDrop
Draggable
, the Droppable
, and the event<img id="product1" src="images/shirt.png" alt="shirt" /> <img id="product2" src="images/cup.png" alt="cup" /> <div id="droptarget"></div>
document.observe("dom:loaded", function() { new Draggable("product1"); new Draggable("product2"); Droppables.add("droptarget", {onDrop: productDrop}); }); function productDrop(drag, drop, event) { alert("You dropped " + drag.id); }
Sortable
Sortable.create(element or id of list, { options } );
ul
, ol
) as being able to be dragged into any orderDraggable
s and Droppable
stag
,
only
,
overlap
,
constraint
,
containment
,
format
,
handle
,
hoverclass
,
ghosting
,
dropOnEmpty
,
scroll
,
scrollSensitivity
,
scrollSpeed
,
tree
,
treeTag
Sortable.destroy
on itSortable
demo<ol id="simpsons"> <li id="simpsons_0">Homer</li> <li id="simpsons_1">Marge</li> <li id="simpsons_2">Bart</li> <li id="simpsons_3">Lisa</li> <li id="simpsons_4">Maggie</li> </ol>
document.observe("dom:loaded", function() { Sortable.create("simpsons"); });
Sortable
list eventsevent | description |
---|---|
onChange
|
when any list item hovers over a new position while dragging |
onUpdate
|
when a list item is dropped into a new position (more useful) |
document.observe("dom:loaded", function() { Sortable.create("simpsons", { onUpdate: listUpdate }); });
onChange
handler function receives the dragging element as its parameteronUpdate
handler function receives the list as its parameterSortable
list events example
document.observe("dom:loaded", function() {
Sortable.create("simpsons", {
onUpdate: listUpdate
});
});
function listUpdate(list) {
// can do anything I want here; effects, an Ajax request, etc.
list.shake();
}
Sortable
eventsonUpdate
to work, each li
must have an id
of the form listID_index
<ol id="simpsons"> <li id="simpsons_0">Homer</li> <li id="simpsons_1">Marge</li> <li id="simpsons_2">Bart</li> <li id="simpsons_3">Lisa</li> <li id="simpsons_4">Maggie</li> </ol>
Sortable.create
on the list again to fix itScriptaculous offers ways to make a text box that auto-completes based on prefix strings:
Autocompleter.Local
:
auto-completes from an array of choices
Ajax.Autocompleter
:
fetches and displays list of choices using Ajax
Autocompleter.Local
new Autocompleter.Local( element or id of text box, element or id of div to show completions, array of choices, { options } );
div
to store the auto-completion matches
ul
that you can style with CSSclass
of selected
{
}
choices
, partialSearch
, fullSearch
, partialChars
, ignoreCase
Autocompleter.Local
demo<input id="bands70s" size="40" type="text" /> <div id="bandlistarea"></div>
document.observe("dom:loaded", function() { new Autocompleter.Local( "bands70s", "bandlistarea", ["ABBA", "AC/DC", "Aerosmith", "America", "Bay City Rollers", ...], {} ); });
<input id="bands70s" size="40" type="text" /> <div id="bandlistarea"></div>
#bandlistarea {
border: 2px solid gray;
}
/* 'selected' class is given to the autocomplete item currently chosen */
#bandlistarea .selected {
background-color: pink;
}
Ajax.Autocompleter
new Ajax.Autocompleter( element or id of text box, element or id of div to show completions, url, { options } );
ul
with li
elements in itparamName
,
tokens
,
frequency
,
minChars
,
indicator
,
updateElement
,
afterUpdateElement
,
callback
,
parameters
Ajax.InPlaceEditor
new Ajax.InPlaceEditor(element or id, url, { options } );
okButton
,
okText
,
cancelLink
,
cancelText
,
savingText
,
clickToEditText
,
formId
,
externalControl
,
rows
,
onComplete
,
onFailure
,
cols
,
size
,
highlightcolor
,
highlightendcolor
,
formClassName
,
hoverClassName
,
loadTextURL
,
loadingText
,
callback
,
submitOnBlur
,
ajaxOptions
onEnterHover
,
onLeaveHover
,
onEnterEditMode
,
onLeaveEditMode
Ajax.InPlaceCollectionEditor
new Ajax.InPlaceCollectionEditor(element or id, url, { collection: array of choices, options } );
Ajax.InPlaceEditor
that gives a collection of choicescollection
option whose value is an array of strings to choose fromAjax.InPlaceEditor
method | description |
---|---|
Sound.play("url");
|
plays a sound/music file |
Sound.disable();
|
stops future sounds from playing (doesn't mute any sound in progress) |
Sound.enable();
|
re-enables sounds to be playable after a call to Sound.disable()
|
Sound.play("music/java_rap.mp3"); Sound.play("music/wazzaaaaaap.wav");
Sound.play('', {replace: true});
new Control.Slider("id of knob", "id of track", {options});
Builder
- convenience class to replace document.createElement
:
var img = Builder.node("img", { src: "images/lolcat.jpg", width: 100, height: 100, alt: "I can haz Scriptaculous?" }); $("main").appendChild(img);
abort
|
blur
|
change
|
click
|
dblclick
|
error
|
focus
|
keydown
|
keypress
|
keyup
|
load
|
mousedown
|
mousemove
|
mouseout
|
mouseover
|
mouseup
|
reset
|
resize
|
select
|
submit
|
unload
|
click
event (onclick
) is just one of many events that can be handled
element.onevent = function; element.observe("event", function);
// call the playNewGame function when the Play button is clicked
$("play").observe("click", playNewGame);
observe
method (added by Prototype)
observe
substitutes for addEventListener
(not supported by IE)
event
object
function name(event) {
// an event handler function ...
}
method / property name | description |
---|---|
type
|
what kind of event, such as "click" or "mousedown"
|
element() *
|
the element on which the event occurred |
stop() **
|
cancels an event |
stopObserving()
|
removes an event handler |
srcElement
and which
properties
return false;
, stopPropagation
, etc.
click
|
user presses/releases mouse button on the element |
dblclick
|
user presses/releases mouse button twice on the element |
mousedown
|
user presses down mouse button on the element |
mouseup
|
user releases mouse button on the element |
mouseover
|
mouse cursor enters the element's box |
mouseout
|
mouse cursor exits the element's box |
mousemove
|
mouse cursor moves around within the element's box |
The event
passed to a mouse handler has these properties:
property/method | description |
---|---|
clientX , clientY
|
coordinates in browser window |
screenX , screenY
|
coordinates in screen |
offsetX , offsetY
|
coordinates in element (non-standard) |
pointerX() , pointerY() *
|
coordinates in entire web page |
isLeftClick() **
|
true if left button was pressed
|
pageX
and pageY
button
and which
<pre id="target">Move the mouse over me!</pre>
window.onload = function() { $("target").observe("mousemove", showCoords); }; function showCoords(event) { $("target").innerHTML = "pointer: (" + event.pointerX() + ", " + event.pointerY() + ")\n" + "screen : (" + event.screenX + ", " + event.screenY + ")\n" + "client : (" + event.clientX + ", " + event.clientY + ")"; }
Move the mouse over me!
this
this.fieldName // access field this.fieldName = value; // modify field this.methodName(parameters); // call method
window
object (so this
=== window
)
window
this
keyword refers to the current object
window.onload = function() { $("textbox").observe("mouseout", booyah); // bound to text box here $("submit").observe("click", booyah); // bound to submit button here }; function booyah() { // booyah knows what object it was called on this.value = "booyah"; }
this
(rather than the window
)
this
<fieldset> <label><input type="radio" name="ducks" value="Huey" /> Huey</label> <label><input type="radio" name="ducks" value="Dewey" /> Dewey</label> <label><input type="radio" name="ducks" value="Louie" /> Louie</label> </fieldset>
function processDucks() {if ($("huey").checked) { alert("Huey is checked!"); } else if ($("dewey").checked) { alert("Dewey is checked!"); } else { alert("Louie is checked!"); }alert(this.value + " is checked!"); }
name | description |
---|---|
load ,
unload
|
the browser loads/exits the page |
resize
|
the browser window is resized |
error
|
an error occurs when loading a document or an image |
contextmenu
|
the user right-clicks to pop up a context menu |
window
object. An alternative to window.onload
:
window.onload = function() { ... };document.observe("dom:loaded", function() { // attach event handlers, etc. });
name | description |
---|---|
keydown
|
user presses a key while this element has keyboard focus |
keyup
|
user releases a key while this element has keyboard focus |
keypress
|
user presses and releases a key while this element has keyboard focus |
focus
|
this element gains keyboard focus |
blur
|
this element loses keyboard focus |
select
|
this element's text is selected or deselected) |
property name | description |
---|---|
keyCode
|
ASCII integer value of key that was pressed (convert to char with String.fromCharCode )
|
altKey , ctrlKey , shiftKey
|
true if Alt/Ctrl/Shift key is being held
|
Event.KEY_BACKSPACE
|
Event.KEY_DELETE
|
Event.KEY_DOWN
|
Event.KEY_END
|
Event.KEY_ESC
|
Event.KEY_HOME
|
Event.KEY_LEFT
|
Event.KEY_PAGEDOWN
|
Event.KEY_PAGEUP
|
Event.KEY_RETURN
|
Event.KEY_RIGHT
|
Event.KEY_TAB
|
Event.KEY_UP
|
event name | description |
---|---|
submit
|
form is being submitted |
reset
|
form is being reset |
change
|
the text or state of a form control has changed |
activate |
clear |
disable |
enable |
focus |
getValue |
present |
select |
$F("formID")["name"]
$F("controlID")
$F
function returns the value of a form control with the given id
if ($F("username").length < 4) { $("username").clear(); $("login").disable(); }
<form id="exampleform" action="http://foo.com/foo.php">...</form>
window.onload = function() {
$("exampleform").observe("submit", checkData);
};
function checkData(event) {
if ($F("city") == "" || $F("state").length != 2) {
alert("Error, invalid city/state."); // show error message
event.stop();
return false;
}
}
stop
method on the eventUse events to do some cool animations!
We use timer events, with methods setInterval and clearInterval
setInterval(code,millisec)
returns ID of timer event
clearInterval(id_of_setinterval)